//
//  TabBarController.m
//  YTwallet
//
//  Created by hadrlog on 2019/2/22.
//  Copyright © 2019年 hadrlog. All rights reserved.
//

#import "TabBarController.h"
#import "MineViewController.h"
#import "ShareViewController.h"
#import "HomeViewController.h"
#import "NavigateViewController.h"
#import "LoginViewController.h"
@interface TabBarController ()<UITabBarControllerDelegate>

@end

@implementation TabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 首页
    HomeViewController *homeVC = [[HomeViewController alloc]init];
    [self setViewController:homeVC title:@"首页" image:@"bottom_ic_home_normal" selectImage:@"bottom_ic_home_pressed"];
    // 分享
    ShareViewController *share =[[ShareViewController alloc] init];
     [self setViewController:share title:@"分享" image:@"bottom_ic_share_normal" selectImage:@"bottom_ic_share_pressed"];
    // 我的
    MineViewController *mineVC = [[MineViewController alloc]init];
    [self setViewController:mineVC title:@"我的" image:@"bottom_ic_me_normal" selectImage:@"bottom_ic_me_pressed"];
    //设置文字样式
    [self formWLab];
    
    self.delegate = self;
    
    //设置tabbar背景颜色
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    [UITabBar appearance].translucent = NO;
    //去掉tabbar顶部线
    /*
    CGRect rect = CGRectMake(0, 0, ETScreenW, ETScreenH);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setBackgroundImage:img];
    [self.tabBar setShadowImage:img];
     */
    
    
}
-(void)formWLab
{
    //设置文字样式
    //选中样式
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:10.0];
    selectTextAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:91.0f/255.0f green:113.0f/255.0f blue:255.0f/255.0f alpha:1.0f];
    [[UITabBarItem appearance]setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    //默认样式
    NSMutableDictionary *TextAttrs = [NSMutableDictionary dictionary];
    TextAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:10.0];
    TextAttrs[NSForegroundColorAttributeName] = [UIColor colorWithRed:102.0f/255.0f green:102.0f/255.0f blue:102.0f/255.0f alpha:1.0f];
    [[UITabBarItem appearance]setTitleTextAttributes:TextAttrs forState:UIControlStateNormal];
    
}

#pragma mark - 添加子控制器
-(void)setViewController:(UIViewController *)viewController title:(NSString *)title image:(NSString *)image selectImage:(NSString *)selectImage {
    static NSInteger index = 0;
    viewController.tabBarItem.title = title;
    viewController.title = title;
    viewController.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.selectedImage = [[UIImage imageNamed:selectImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController.tabBarItem.tag = index;
    index++;
    NavigateViewController *nav = [[NavigateViewController alloc]initWithRootViewController:viewController];
    [self addChildViewController:nav];
}

#pragma mark tabbar点击方法

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    
    if ([viewController.tabBarItem.title isEqualToString:@"分享"]) {
        
        if (![AppTools isLogin]) {
            //跳转到登录页面
            LoginViewController *loginVC = [[LoginViewController alloc] init];
            NavigateViewController *nav = [[NavigateViewController alloc] initWithRootViewController:loginVC];
            [self presentViewController:nav animated:YES completion:nil];
            return NO;
        }else if([AppTools checkLoginResponseAllState] == ETNOAutonym){
            UIViewController *topVC = [self findCurrentViewController];
            [(BaseViewController *)topVC showFailedResultTips:@"用户未认证，请去个人中心认证" animated:YES forSeconds:FAIL_SHOW_TIME_DEF];
               
                return NO;
            
            
        }else
        {
            return YES;
        }
       
    }else
    {
        return YES;
    }
    
}
- (UIViewController *)findCurrentViewController
{
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    UIViewController *topViewController = [window rootViewController];
    
    while (true) {
        
        if (topViewController.presentedViewController) {
            
            topViewController = topViewController.presentedViewController;
            
        } else if ([topViewController isKindOfClass:[UINavigationController class]] && [(UINavigationController*)topViewController topViewController]) {
            
            topViewController = [(UINavigationController *)topViewController topViewController];
            
        } else if ([topViewController isKindOfClass:[UITabBarController class]]) {
            
            UITabBarController *tab = (UITabBarController *)topViewController;
            topViewController = tab.selectedViewController;
            
        } else {
            break;
        }
    }
    return topViewController;
}

@end
